﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.InteropServices;

using Microsoft.SqlServer.Management.UI.VSIntegration;
using Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer;


namespace XEvents.Addin
{
    public partial class XEventSessionExplorer_broke : UserControl
    {
        private string currentServerName = string.Empty;
        public XEventSessionExplorer_broke()
        {
            InitializeComponent();
        }

        private void XEventSessionExplorer_Load(object sender, EventArgs e)
        {
            UpdateExplorerInfo();

            // Object Explorer SelectionChangedEvent 
            IObjectExplorerEventProvider provider = (IObjectExplorerEventProvider)ServiceCache.GetObjectExplorer().GetService(typeof(IObjectExplorerEventProvider));
            provider.SelectionChanged += new NodesChangedEventHandler(Provider_SelectionChanged);
        }

        void Provider_SelectionChanged(object sender, NodesChangedEventArgs args)
        {
            UpdateExplorerInfo();
        }

        internal INodeInformation SelectedNode
        {
            get
            {
                int count;
                INodeInformation[] informationArray;
                ServiceCache.GetObjectExplorer().GetSelectedNodes(out count, out informationArray);
                if (count > 0)
                {
                    return informationArray[0];
                }
                return null;
            }
        }

        private void UpdateExplorerInfo()
        {
            this.xEventSessionExplorer1.ResumeLayout();
            // Nothing selected
            if (SelectedNode == null)
                return;

            // Don't fire if Server wasn't changed.
            if (currentServerName == SelectedNode.Connection.ServerName)
                return;

            currentServerName = SelectedNode.Connection.ServerName;
            xEventSessionExplorer1 = new XEvents.UIControls.XEventSessionExplorerControl();

            XEvents.Library.SqlServer server;

            try
            {
                if (SelectedNode.Connection.UseIntegratedSecurity)
                    server = new XEvents.Library.SqlServer(SelectedNode.Connection.ServerName);
                else
                    server = new XEvents.Library.SqlServer(SelectedNode.Connection.ServerName, SelectedNode.Connection.UserName, SelectedNode.Connection.Password);

            }
            catch
            {
                return;
            }
            xEventSessionExplorer1.AddServer(server);
            xEventSessionExplorer1.Load_Form();

 
            //// check if the current ObjectExplorer selection is a database
            //Regex databaseRegex = new Regex(@"^Server\[@Name='(?<server>[^']*)'\]/Database\[@Name='(?<database>[^']*)'\]");
            //Match match = databaseRegex.Match(node.Context);
            //if (match != null && match.Groups.Count == 3)
            //{
            //    toolStripLabelServer.Text = match.Groups["server"].Value;
            //    toolStripLabelDatabase.Text = match.Groups["database"].Value;

            //    _database = match.Groups["database"].Value;
            //}
        }



    }
}
